<template>
  <div>
    <el-form :inline="true" :model="query" class="demo-form-inline">
      <el-form-item label="组织名称">
        <el-input v-model="query.orgName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="queryLoading" @click="getTree">查询</el-button>
        <el-button @click="reset">重置</el-button>
        <el-button v-if="$hasApiPerm('org:add')" type="success" @click="showAdd()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="treeData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      :header-cell-style="{textAlign : 'center'}"
      :cell-style="{textAlign : 'center'}"
      :tree-props="{children: 'children'}"
    >
      <el-table-column
        prop="orgName"
        label="组织名称"
      >
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="160"
      >
        <template slot-scope="{row}">
          {{ resolveTime(row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
      >
        <template slot-scope="{row}">
          <el-button v-if="$hasApiPerm('org:get')" type="text" size="small" @click="showView(row.id)">查看</el-button>
          <el-button v-if="$hasApiPerm('org:add')" type="text" size="small" @click="showAdd(row.id)">新增</el-button>
          <el-button v-if="$hasApiPerm('org:update')" type="text" size="small" @click="showUpdate(row.id)">编辑</el-button>
          <el-button v-if="$hasApiPerm('org:del')" type="text" size="small" @click="ensureDelete(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <opt-organization :dialog-visible.sync="optDialog" :opt-flag="optFlag" :pid="pid" :org-id="orgId"
                      @refresh="getTree"
    />
  </div>
</template>

<script>
import organizationApi from '@/api/system/organizationApi'
import OptOrganization from '@/views/system/organization/components/OptOrganization.vue'
import moment from 'moment'

export default {
  name: 'OrganizationList',
  components: { OptOrganization },
  data() {
    return {
      query: {},
      queryLoading: false,
      treeData: [],
      optDialog: false,
      optFlag: '',
      pid: null,
      orgId: null
    }
  },
  created() {
    this.getTree()
  },
  methods: {
    getTree() {
      this.queryLoading = true
      organizationApi.getTreeByDataPerm(this.query).then(res => {
        this.treeData = res.data
      }).finally(() => {
        this.queryLoading = false
      })
    },
    resolveTime(time) {
      return moment(time).format('YYYY-MM-DD HH:mm:ss')
    },
    showView(orgId) {
      this.optDialog = true
      this.optFlag = 'view'
      this.pid = null
      this.orgId = orgId
    },
    showAdd(pid = null) {
      this.optDialog = true
      this.optFlag = 'add'
      this.pid = pid
      this.orgId = null
    },
    showUpdate(orgId) {
      this.optDialog = true
      this.optFlag = 'update'
      this.pid = null
      this.orgId = orgId
    },
    ensureDelete(orgId) {
      this.$confirm('确定执行删除操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteOpt(orgId)
      }).catch(() => {
      })
    },
    deleteOpt(orgId) {
      organizationApi.delete(orgId).then(res => {
        this.$message({
          message: '操作成功！',
          type: 'success'
        })
        this.getTree()
      })
    },
    reset() {
      this.query = {}
    }
  }
}
</script>

<style scoped>

</style>
